<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            position: absolute;
            left: 0;
            top: 0;
            background-color: aqua;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        //1.方块的运动
        //让方块运动到300px
        //先获取方块
        /*  var boxEle = document.querySelector(".box");
          var timer = setInterval(function() {
              //（设置时间间隔）
              var num = parseInt(getComputedStyle(boxEle)["left"]);

              if (num == 300) {
                  console.log("停止了");
                  clearTimeout(timer);
              } else {
                  num += 10;
                  boxEle.style.left = num + "px";
              }
          }, 100); */

        // 2.方块运动封装成函数
        function move(ele, target, dir, cb) {
            var timer = setInterval(function() {
                // 获取 方块的left值；
                var num = parseInt(getComputedStyle(ele)[dir]);
                // console.log(num);
                if (num == target) {
                    console.log("停止了");
                    clearTimeout(timer);
                    // if(cb){
                    //     cb();
                    // }
                    cb && cb();
                } else {
                    num += 10;
                    ele.style[dir] = num + "px";
                }
            }, 100);
        }

        var boxEle = document.querySelector(".box");
        move(boxEle, 300, "left", function() {
            console.log("传入的函数");
            move(boxEle, 300, "top");
        }); //右边
        // move(boxEle,300,"top");  //下边
    </script>
</body>

</html>